//
//  LXCarListView.m
//  lixing
//
//  Created by bancheng on 2022/6/13.
//

#import "LXCarListView.h"
#import "LXCarListTableViewCell.h"
#import "CheLiangDetailViewController.h"
@interface LXCarListView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic) NSMutableArray *data;
@property(nonatomic) NSInteger page;
@property(nonatomic) NSString *searchStr;
@property(nonatomic) NSInteger type;
@end

@implementation LXCarListView

- (instancetype)initWithFrame:(CGRect)frame WithType:(int)type{
    if (self = [super initWithFrame:frame]) {
        
        self.type = type;
        
        self.data = [NSMutableArray array];
        
        [self addSubview:self.tableView];
        
        
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self loadData];
            
        }];
        [self.tableView.mj_header beginRefreshing];
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page++;
            [self loadData];
        }];
        
        

        
    }
    return self;
}

-(void)loadData{
    // hireFlag 1已租 0空闲
    NSMutableDictionary *dic = @{
        @"page":@(self.page),
        @"rows":@(20),
        @"type":@(self.type)
    }.mutableCopy;
    
    
    if (!StringIsEmpty(self.searchStr)) {
        [dic setValue:self.searchStr forKey:@"carNum"];
    }
    
    [MSNetworkManager postRevertUrl:CarList_URL params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        if (isSuccess) {
            
            if (self.page==1) {
                [self.data removeAllObjects];
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            
            NSArray *cacheArray = resonseObj[@"list"];
            [self.data addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:cacheArray]];
            
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            [self.tableView reloadData];
            [self isHaveEmptyData];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:[BaseViewController getCurrentVC]];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier;
    NSInteger index = 0;;
    if (self.type == 13) {
        identifier = @"cell3";
        index = 2;
    }
    else {
        identifier = @"cell1";
        index = 0;
    }
    LXCarListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCarListTableViewCell class]) owner:self options:nil] objectAtIndex:index];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CheLiangModel *model = self.data[indexPath.row];
    if (self.type == 13) {
        cell.rentModel = model;
    }
    else {
        cell.carModel = model;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CheLiangModel  *model = self.data[indexPath.row];
    CheLiangDetailViewController *vc = [[CheLiangDetailViewController alloc] init];
    vc.detailModel = model;
    BaseViewController *v = (BaseViewController *)[BaseViewController getCurrentVC];
    [v pushToNextController:vc];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 120;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}
@end
